import {formatMessage} from '../intl';
import {warrantyPolicyType as WarrantyPolicyType} from '../../Enum';
import {groupBy, pickBy} from 'lodash';

export const validateAdd = (data, id) => {
    const {code, name, dealerId, brandId, startTime, items = []} = data;
    const msgs = [];
    if(!brandId && !id)
        msgs.push(
            formatMessage({
                id: 'validate.brandId',
                defaultMessage: '品牌'
            })
        );
    if(!dealerId && !id)
        msgs.push(
            formatMessage({
                id: 'validate.dealerId',
                defaultMessage: '总代'
            })
        );
    if(!code && !id)
        msgs.push(
            formatMessage({
                id: 'validate.code',
                defaultMessage: '编号'
            })
        );
    if(!name)
        msgs.push(
            formatMessage({
                id: 'validate.name',
                defaultMessage: '名称'
            })
        );
    if(!startTime)
        msgs.push(
            formatMessage({
                id: 'validate.startTime',
                defaultMessage: '启用日期'
            })
        );
    if(!items.length)
        msgs.push(
            formatMessage({
                id: 'validate.details',
                defaultMessage: '条款信息'
            })
        );
    if(msgs.length)
        return {
            message: `${msgs.join(
                formatMessage({
                    id: 'validate.comma',
                    defaultMessage: '、'
                })
            )}${formatMessage({
                id: 'validate.notEmpty',
                defaultMessage: '不能为空'
            })}`,
            detailId: '',
            isOk: false
        };
    for(let i = 0; i < items.length; i++) {
        const {
            id: itemId,
            warrantyPolicyType,
            warrantyMileage,
            warrantyMonth,
            vehicles = []
        } = items[i];
        if(!warrantyPolicyType)
            msgs.push(
                formatMessage({
                    id: 'validate.warrantyPolicyType',
                    defaultMessage: '保养类型'
                })
            );
        if(!warrantyMileage)
            msgs.push(
                formatMessage({
                    id: 'validate.warrantyMileage',
                    defaultMessage: '索赔里程'
                })
            );
        if(!warrantyMonth)
            msgs.push(
                formatMessage({
                    id: 'validate.warrantyMonth',
                    defaultMessage: '索赔月份'
                })
            );
        if(!vehicles.length)
            msgs.push(
                formatMessage({
                    id: 'validate.vehicles',
                    defaultMessage: '车型车辆清单'
                })
            );
        if(msgs.length)
            return {
                message: `${msgs.join(
                    formatMessage({
                        id: 'validate.comma',
                        defaultMessage: '、'
                    })
                )}${formatMessage({
                    id: 'validate.notEmpty',
                    defaultMessage: '不能为空'
                })}`,
                detailId: itemId,
                isOk: false
            };
        const vehiclePropertyObj = {};
        for(let j = 0; j < vehicles.length; j++) {
            if(warrantyPolicyType === WarrantyPolicyType.标准) {
                const {productCategoryId, productCategoryCode, vehicleProperty, vehicleClaimType} = vehicles[j];
                if(!vehicleProperty)
                    return {
                        message: `${formatMessage({
                            id: 'validate.vehicleProperty.notEmpty',
                            defaultMessage: '车辆属性不能为空'
                        })}`,
                        detailId: itemId,
                        isOk: false
                    };
                if(!vehicleClaimType)
                    return {
                        message: `${formatMessage({
                            id: 'validate.vehicleClaimType.notEmpty',
                            defaultMessage: '索赔类别不能为空'
                        })}`,
                        detailId: itemId,
                        isOk: false
                    };
                if(!vehiclePropertyObj[productCategoryId])
                    vehiclePropertyObj[productCategoryId] = [vehicleProperty];
                else if(vehiclePropertyObj[productCategoryId].includes(vehicleProperty))
                    return {
                        message: formatMessage({
                            id: 'validate.productCategoryCode.notEmpty',
                            defaultMessage: '{productCategoryCode}的车辆属性不能相同'
                        }, {productCategoryCode}),
                        detailId: itemId,
                        isOk: false
                    };
                else
                    vehiclePropertyObj[productCategoryId].push(vehicleProperty);
            }
            if(warrantyPolicyType === WarrantyPolicyType.特殊)
                if(Object.keys(pickBy(groupBy(vehicles, 'vehicleId'), values => values.length > 1)).length > 1)
                    return {
                        message: `${formatMessage({
                            id: 'validate.vehicleProperty.notEmpty1',
                            defaultMessage: '车辆不能重复'
                        })}`,
                        detailId: itemId,
                        isOk: false
                    };
            const {materials = []} = vehicles[j];
            for(let k = 0; k < materials.length; k++) {
                if(!materials[k].partWarrantyCategoryId)
                    msgs.push(
                        formatMessage({
                            id: 'validate.partWarrantyCategoryId',
                            defaultMessage: '配件索赔分类标准'
                        })
                    );
                if(!materials[k].warrantyDuration)
                    msgs.push(
                        formatMessage({
                            id: 'validate.warrantyDuration',
                            defaultMessage: '索赔时间（月）'
                        })
                    );
                if(!materials[k].warrantyMileage)
                    msgs.push(
                        formatMessage({
                            id: 'validate.warrantyMileage',
                            defaultMessage: '索赔里程'
                        })
                    );
                if(msgs.length)
                    return {
                        message: `${msgs.join(
                            formatMessage({
                                id: 'validate.comma',
                                defaultMessage: '、'
                            })
                        )}${formatMessage({
                            id: 'validate.notEmpty',
                            defaultMessage: '不能为空'
                        })}`,
                        detailId: itemId,
                        isOk: false
                    };
            }
        }
    }
            
    return false;
};
